# main.py
import uvicorn
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from app.core.config import TORTOISE_ORM
from tortoise.contrib.fastapi import register_tortoise
from app.api.news import router as news_router
from app.api.resource import router as resource_router
from app.api.honor import router as honor_router
from app.api.student import router as student_router
from app.api.teacher import router as teacher_router
from app.api.course import router as course_router
from app.api.agent import router as agent_router
from app.api.signup import router as signup_router
from app.api.news import router as news_router
from app.api.honor import router as honor_router
from app.api.auth import router as auth_router



import os

app = FastAPI(
    title="农民技能提升就业培训大数据平台API",
    version="1.0.0",
    description="农民技能提升就业培训大数据平台后端API"
)

# CORS配置
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # 开发环境允许所有来源
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)


# 创建上传目录（如果不存在）
os.makedirs("uploads", exist_ok=True)
# 挂载静态文件目录以提供上传的图片访问
app.mount("/uploads", StaticFiles(directory="uploads"), name="uploads")





#/////////////////////
app.include_router(news_router, prefix="/news") # 新闻接口
app.include_router(student_router, prefix="/students")
app.include_router(teacher_router, prefix="/teachers")
app.include_router(course_router, prefix="/courses")
app.include_router(agent_router, prefix="/agents")
app.include_router(signup_router, prefix="/signups")
app.include_router(honor_router, prefix="/honors")
app.include_router(auth_router, prefix="/auth")
app.include_router(resource_router, prefix="/resources")



register_tortoise(
    app,
    config=TORTOISE_ORM,
    # generate_schemas=True,
    add_exception_handlers=True
)

# 基础路由
@app.get("/")
async def root():
    print("请求根路径")
    return {
        "message": "农民技能提升就业培训大数据平台API",
        "version": "1.0.0",
        "status": "running"
    }

#启动命令：uvicorn main:app --reload




if __name__ == "__main__":
    uvicorn.run(
        "main:app",
        host="127.0.0.1",
        port=8000,
        reload=True,
    )
